/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode *f = head,* b = head;
        if (head==nullptr||b->next==nullptr) return head;
        f = f->next;
        //front:如果和back重复，设为temp，front后移，释放temp，back指向front
        //back:前面包括当下都是不重复的
        //front永远指向back,紧挨着
        while(f!=nullptr){
            //说明此时back到达最后一个节点
            if (b->val==f->val){
                ListNode * temp = f;
                f = f->next;
                b->next = f;
                delete temp;
            }else{
                b = f;
                f = f->next;
            }
        }
        return head;

    }
};